const fs = require('fs')
const path = require('path')

 // 1.创建文件夹
const dirname = './lyc'
if(!fs.existsSync(dirname)) {
  console.log(dirname,' not found')
  fs.mkdir(dirname, err => {
    if(err) {
      console.log(err)
      return
    } else {
      console.log('create success')
    }
  })
} else {
  console.log(dirname)
}

 // 2.读取文件夹中的所有文件
//  fs.readdir(dirname, (err, files) => {
//   console.log(files)
//  })

function getFiles(dirname) {
  fs.readdir(dirname, {withFileTypes: true}, (err, files) => {
    files.forEach(file => {
      if(file.isDirectory()) {
        const filepath = path.resolve(dirname, file.name)
        getFiles(filepath)
      } else {
        console.log(file.name)
      }
    })
 })
}

// getFiles(dirname)

 //3.文件夹重命名
fs.rename('./lyc', './mx', err => {
  if(err) console.log(err)
})